//
//  QPointsTwoVC.m
//  QoilPublic
//
//  Created by 杨凯 on 2017/3/20.
//  Copyright © 2017年 杨凯. All rights reserved.
//

#import "QPointsTwoVC.h"
#import "QPointsObtainVC.h"
#import "QPointsConsumeVC.h"
#define btnWidth ([UIScreen mainScreen].bounds.size.width)/2

@interface QPointsTwoVC ()<UIScrollViewDelegate>
@property(nonatomic,strong)UIScrollView *bScrollView;

@property(nonatomic,strong)UIView *btitleView;

@property(nonatomic,strong)UIView *selectView;

@end

@implementation QPointsTwoVC{
    NSArray *_titleArr;
    NSMutableArray *_btnArr;
    //为了移除通知*********
    QPointsObtainVC *_vc1;
    QPointsConsumeVC *_vc2;

}

- (void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:_vc1];
    [[NSNotificationCenter defaultCenter] removeObserver:_vc2];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"积分详情";
    self.automaticallyAdjustsScrollViewInsets = NO;
    _titleArr = @[@"积分获取",@"积分消耗"];
    _btnArr = [NSMutableArray array];
    [self createUI];
    [self addtitleButton];
    [self addController];
    
    QPointsObtainVC *vc = (QPointsObtainVC *)[self.childViewControllers firstObject];
    vc.view.frame = self.bScrollView.bounds;
    [self.bScrollView addSubview:vc.view];
    self.view.clipsToBounds = YES;

}
- (void)createUI{
    WS(wSelf);
    [self.view addSubview:self.btitleView];
    [_btitleView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(wSelf.view.mas_top).offset((kNavHeight+kStatusHeight));
        make.left.equalTo(wSelf.view.mas_left).offset(0);
        make.right.equalTo(wSelf.view.mas_right).offset(0);
        make.height.equalTo(@45);
    }];
    self.selectView.frame = CGRectMake(0, 43, btnWidth, 2);
    [self.btitleView addSubview:self.selectView];
    
    UIView *lineView = [[UIView alloc] init];
    lineView.backgroundColor = [QColorUtility defaultLineColor];
    [self.btitleView addSubview:lineView];
    [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(wSelf.view.mas_left).offset(0);
        make.right.equalTo(wSelf.view.mas_right).offset(0);
        make.height.equalTo(@0.5);
        make.bottom.equalTo(wSelf.btitleView.mas_bottom).offset(0);
    }];
    
    [self.view addSubview:self.bScrollView];
    [_bScrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_btitleView.mas_bottom).offset(0);
        make.left.equalTo(wSelf.view.mas_left).offset(0);
        make.right.equalTo(wSelf.view.mas_right).offset(0);
        make.bottom.equalTo(wSelf.view.mas_bottom).offset(0);
    }];
    self.bScrollView.contentSize = CGSizeMake(_titleArr.count*kScreenWidth, 0);
    
}

#pragma mark --- 添加button
- (void)addtitleButton{
    for (int i=0; i<_titleArr.count; ++i) {
        UIButton *button = [QViewUtility createButtonWithFrame:CGRectMake(i*btnWidth,0, btnWidth,43) font:Ksize15 title:_titleArr[i] titleColor:[QColorUtility defaultBackColor] backgroundColor:[UIColor whiteColor]];
        button.tag = i;
        [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
        if(button.tag == 0){
            [button setTitleColor:[QColorUtility defaultBlueColor] forState:UIControlStateNormal];
        }else{
            [button setTitleColor:[QColorUtility defaultBackColor] forState:UIControlStateNormal];
        }
        [self.btitleView addSubview:button];
        [_btnArr addObject:button];
    }
}

#pragma mark --- 添加控制器
- (void)addController{
    QPointsObtainVC *vc1 = [[QPointsObtainVC alloc] init];
    _vc1 = vc1;
    [self addChildViewController:vc1];
    
    QPointsConsumeVC *vc2 = [[QPointsConsumeVC alloc] init];
    _vc2 = vc2;
    [self addChildViewController:vc2];
    
}

- (void)buttonAction:(UIButton *)btn{
    CGFloat offsetX = btn.tag * self.bScrollView.frame.size.width;
    CGFloat offsetY = self.bScrollView.contentOffset.y;
    CGPoint offset = CGPointMake(offsetX, offsetY);
    [self.bScrollView setContentOffset:offset animated:YES];
}

#pragma mark --- scrollviewDelegate
//代码调用
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView{
    int index = scrollView.contentOffset.x/self.bScrollView.frame.size.width;
    [[NSNotificationCenter defaultCenter] postNotificationName:kNotificationscrollsToTop object:nil userInfo:@{kTopKey:[NSString stringWithFormat:@"%d",index]}];
    NSLog(@">>>>>>>>%d",index);
    UIViewController *vc = self.childViewControllers[index];
    [self setupButtonColor:index];
    if(vc.view.superview){
        return;
    }else{
        vc.view.frame = scrollView.bounds;
        [self.bScrollView addSubview:vc.view];
    }
}

//手势调用
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    NSUInteger index = scrollView.contentOffset.x/self.bScrollView.frame.size.width;
    [UIView animateWithDuration:0.23 animations:^{
        _selectView.frame = CGRectMake(index*btnWidth, 43, btnWidth, 2);
    }];
    [self scrollViewDidEndScrollingAnimation:scrollView];
}

//滑动调用
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    CGFloat index = (scrollView.contentOffset.x/self.bScrollView.frame.size.width);
    _selectView.frame = CGRectMake(btnWidth * index , 43, btnWidth, 2);
    
    CGFloat scrollViewLeftX = scrollView.contentOffset.x;
    if(scrollViewLeftX < 0){
        self.bScrollView.scrollEnabled = NO;
    }else{
        self.bScrollView.scrollEnabled = YES;
    }
}

//设置button颜色
- (void)setupButtonColor:(NSInteger)index{
    
    for (UIButton *btn in _btnArr) {
        if(btn.tag == index){
            [btn setTitleColor:[QColorUtility defaultBlueColor] forState:UIControlStateNormal];
        }else{
            [btn setTitleColor:[QColorUtility defaultBackColor] forState:UIControlStateNormal];
        }
    }
}

#pragma mark --- get/set
- (UIScrollView *)bScrollView{
    if (!_bScrollView) {
        _bScrollView = [[UIScrollView alloc] init];
        _bScrollView.showsHorizontalScrollIndicator = NO;
        _bScrollView.showsVerticalScrollIndicator = NO;
        _bScrollView.pagingEnabled = YES;
        _bScrollView.delegate = self;
        _bScrollView.scrollsToTop = NO;
    }
    return _bScrollView;
}


- (UIView *)btitleView{
    if (!_btitleView) {
        _btitleView = [[UIView alloc] init];
        _btitleView.backgroundColor = [UIColor colorWithWhite:1 alpha:0.7];
    }
    return _btitleView;
}

- (UIView *)selectView{
    if (!_selectView) {
        _selectView = [[UIView alloc] init];
        _selectView.backgroundColor = [QColorUtility defaultBlueColor];
    }
    return _selectView;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
